package com.flash.controller.user;

import com.flash.dto.OrdersPaymentDTO;
import com.flash.dto.OrdersSubmitDTO;
import com.flash.result.PageResult;
import com.flash.result.Result;
import com.flash.service.OrderService;
import com.flash.vo.OrderPaymentVO;
import com.flash.vo.OrderSubmitVO;
import com.flash.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController("UserOrderController")
@Slf4j
@Api(tags = "订单相关接口")
@RequestMapping("/user/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 用户下单接口
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation("用户下单接口")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) throws IOException {
        log.info("用户下单的数据为：{}",ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        /*直接修改订单状态*/
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    /**
     * 历史订单查询
     * @param page
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping("/historyOrders")
    @ApiOperation("历史订单查询")
    public Result<PageResult> historyOrders(int page, int pageSize, Integer status){
        log.info("历史订单查询");
        PageResult pageResult = orderService.getPageResult(page,pageSize,status);
        return Result.success(pageResult);
    }

    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result cancel(@PathVariable Long id){
        log.info("用户取消订单");
        orderService.cancel(id);
        return Result.success();
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */

    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详情")
    //可以加个缓存
    public Result<OrderVO> orderDetail(@PathVariable("id") Long id){
        log.info("查询订单详情：{}",id);
        OrderVO orderVO = orderService.orderDetail(id);
        redisTemplate.opsForValue().set("OrderDetail" + id,orderVO,30, TimeUnit.DAYS);
        return Result.success(orderVO);
    }

    /**
     * 再来一单
     * @param id
     * @return
     */
    @PostMapping("/repetition/{id}")
    @ApiOperation("用户选择再来一单")
    public Result repetition (@PathVariable("id")Long id){
        log.info("用户选择再来一单");
        orderService.repetition(id);
        return Result.success();
    }

    /**
     * 客户催单
     * @return
     */
    @GetMapping("/reminder/{id}")
    @ApiOperation("客户催单")
    public Result reminder (@PathVariable("id")Long id){
        orderService.reminder(id);

        return Result.success();
    }



}
